"""
@author: 石沙
@date: 2020-12-06
@content：本模块用于定义意图识别器（Intention）
"""
import sys
sys.path.append(r'/home/user10000630/notespace/Chatbot_227_ef9a')
import fasttext as ft
from site_packages.utils.job import DataOp
import logging
import configs.settings as conf
import os
from preprocess.preprocessor import filter_content
import jieba
from site_packages.ml_libs.nlp.stopwords import Stopwords


stopwords = Stopwords()


class Intention:
    """
    意图识别器：
    training：
    """
    true_label = '__label__1'
    
    
    def __init__(self, training=False):
        self.training = training
        if not training:
            self.model = ft.load_model(os.path.join(conf.SAVED_MODEL_PATH, 'intention.model'))

    def train(self, train_file):
        logging.info('准备训练')
        self.model = ft.train_supervised(
            train_file,
            label='__label__',
            dim=200,
            epoch=15,
            lr=0.1,
            wordNgrams=2,
            loss='softmax',
            thread=4,
            verbose=True
        )
        self.model.save_model(os.path.join(conf.SAVED_MODEL_PATH, 'intention.model'))
        logging.info('训练完成，已保存模型')

    def test(self, test_file):
        result = self.model.test(test_file)
        # F1 score
        print(result[1] * result[2] * 2 / (result[1] + result[2]))

    def process_input(self, text):
        text = filter_content(text)
        text = jieba.lcut(text)
        text = stopwords.clean(text)
        return ' '.join(text)

    def predict(self, text):
        fasttext_label, prob = self.model.predict(text)
        fit_this = (fasttext_label[0] == self.true_label)
        return fit_this, prob[0]
        
    def __call__(self, train_file=None, test_file=None, text=None, **kwargs):
        if self.training:
            self.train(train_file)
            self.test(test_file)
        else:
            text = self.process_input(text)
            return self.predict(text)


if __name__ == '__main__':
    # 训练
#     train_file = os.path.join(conf.PROCESSED_DATA_PATH, 'train.csv')
#     test_file = os.path.join(conf.PROCESSED_DATA_PATH, 'test.csv')
    intention = Intention(training=False)
#     intention(train_file, test_file)

    # 测试
    text = '你好, 我要退货'
    intention = Intention(training=False)
    result = intention(text=text)
    print("result:", result)